package cn.hellochaos.generator.controller.web;


import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import cn.hellochaos.generator.entity.dto.ResultBean;
import cn.hellochaos.generator.service.UserService;
import cn.hellochaos.generator.entity.User;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author test
 * @version v1.0
 * @since 2021-01-23
 */
@RestController("webUserController")
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 登录
     */
    @RequestMapping(method = RequestMethod.POST, value = "login")
    public ResultBean<?> login(@RequestBody User user) {

        User user1 = userService.login(user);
        if (user1 != null) {
            return new ResultBean<User>(user1);
        }
        return new ResultBean<User>(ResultBean.BIND_CODE, "输入信息有误！");
    }

    /**
     * 注册
     */
    @RequestMapping(method = RequestMethod.POST, value = "/register")
    public ResultBean<?> register(@RequestBody User user) {
        return new ResultBean<User>(userService.register(user));
    }

    /**
     * 退出登录
     * TODO
     */
    @RequestMapping(method = RequestMethod.GET, value = "/logout/{token}")
    public ResultBean<?> logout(@PathVariable String token) {

//        return new ResultBean<>(userService.getUserById(id));
        return null;
    }

    /**
     * 校验手机号是否存在
     * TODO
     */
    @RequestMapping(method = RequestMethod.GET, value = "/is-mobile-exist/{mobile}")
    public ResultBean<?> isMobileExist(@PathVariable("id") Integer id) {
        return new ResultBean<>(userService.getUserById(id));
    }

    /**
     * 重置密码
     * TODO
     */
    @RequestMapping(method = RequestMethod.GET, value = "/reset-password")
    public ResultBean<?> resetPassword(@PathVariable("id") Integer id) {
        return new ResultBean<>(userService.getUserById(id));
    }

    /*===========================================================================*/

    /**
     * 查询分页数据
     */
    @RequestMapping(method = RequestMethod.GET)
    public ResultBean<?> listByPage(@RequestParam(name = "page", defaultValue = "1") int page,
                                    @RequestParam(name = "pageSize", defaultValue = "10") int pageSize,
                                    @RequestParam(name = "factor", defaultValue = "") String factor) {
        return new ResultBean<>(userService.listUsersByPage(page, pageSize, factor));
    }


    /**
     * 根据id查询
     */
    @RequestMapping(method = RequestMethod.GET, value = "/{id}")
    public ResultBean<?> getById(@PathVariable("id") Integer id) {
        return new ResultBean<>(userService.getUserById(id));
    }

    /**
     * 新增
     */
    @RequestMapping(method = RequestMethod.POST)
    public ResultBean<?> insert(@RequestBody User user) {
        return new ResultBean<>(userService.insertUser(user));
    }

    /**
     * 删除
     */
    @RequestMapping(method = RequestMethod.DELETE, value = "/{id}")
    public ResultBean<?> deleteById(@PathVariable("id") Integer id) {
        return new ResultBean<>(userService.deleteUserById(id));
    }

    /**
     * 修改
     */
    @RequestMapping(method = RequestMethod.PUT)
    public ResultBean<?> updateById(@RequestBody User user) {
        return new ResultBean<>(userService.updateUser(user));
    }
}
